package com.example.cs342messenger;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;



public class DatabaseConnector {

	private SQLiteDatabase database;
	private DatabaseOpenHelper databaseOpenHelper;
	SharedPreferences mPrefs;
	
	public DatabaseConnector(Context context){
		databaseOpenHelper = new DatabaseOpenHelper(context, "Messaging", null, 1);
		mPrefs = context.getSharedPreferences("myPrefs", context.MODE_PRIVATE);
	}
	
	public void open() throws SQLException{
		database = databaseOpenHelper.getReadableDatabase();
	}
	
	public void close(){
		if(database != null)
			database.close();
	}
	
	public List<Comment> getMessages(String s, String r){
		List<Comment> message = new ArrayList<Comment>();
		
		
		
		return message;
	}
	
	public long enterMessage(Comment com){
		ContentValues newRow = new ContentValues();
		newRow.put("sender", com.getUser());
		newRow.put("reciever", com.getReciever());
		newRow.put("message", com.getComment());
		
		long id = database.insert("messages", null, newRow);
		return id;
	}
	
	public List<Comment> populateContacts(String user){
		List<Comment> list = new ArrayList<Comment>();
		Comment com;
		String sql = "Select DISTINCT sender FROM messages WHERE reciever = '" + user + "';";
		Cursor cursor = database.rawQuery(sql, new String[] {});
		
		try{
			if(cursor.moveToFirst()){
				do{
					com = new Comment();
					String str;
					com.setUser(cursor.getString(cursor.getColumnIndex("sender")));
					list.add(com);
					cursor.moveToNext();
					
				}while(!cursor.isAfterLast());
			}
		}catch (RuntimeException e)     
		{
			cursor.close();
			Log.e("PopulateMessage", e.toString(), e);
		}
		
		sql = "Select DISTINCT reciever FROM messages WHERE sender = '" + user + "';";
		cursor = database.rawQuery(sql, new String[] {});
		
		try{
			if(cursor.moveToFirst()){
				do{
					com = new Comment();
					String str;
					com.setUser(cursor.getString(cursor.getColumnIndex("reciever")));
					boolean add = true;
					for(int i = 0; i < list.size(); i++){
						if(com.compareTo(list.get(i)) == 1){
							add = false;
						}
					}
					if(add){
						list.add(com);
					}
					cursor.moveToNext();
					
				}while(!cursor.isAfterLast());
			}
		}catch (RuntimeException e){
			cursor.close();
			Log.e("PopulateMessage", e.toString(), e);
		}
		cursor.close();
		String moreCons = mPrefs.getString("updates", "none");
		if(!moreCons.equals("none")){
			String delims = "[ ]+";
			String[] newCons = moreCons.split(delims);
			for(int i = 0; i < newCons.length; i++){
				com = new Comment();
				com.setUser(newCons[i]);
				boolean add = true;
				for(int j = 0; j < list.size(); j++){
					if(com.compareTo(list.get(j)) == 1){
						add = false;
					}
				}
				if(add){
					list.add(com);
				}
			}
		}
		/*
		if(list.size() == 0){
			com = new Comment();
			com.setComment("empty");
			com.setReciever("jill");
			com.setUser("Bill");
			
			list.add(com);
		}*/
		return list;
		
	}
	
	public List<Comment> populateMessages(String user, String buddy){
		List<Comment> list = new ArrayList<Comment>();
		Comment com;
		//String sql = "SELECT * FROM messages WHERE sender = '" + user + "' AND reciever = '"+ buddy +"';";   //AND sender = '"+ buddy +"' AND reciever = '" + user + "';";
		String sql = "SELECT * FROM messages WHERE (sender = '" + user + "' AND reciever = '"+ buddy +"') or (reciever = '" + user + "' AND sender = '"+ buddy +"') ;";
		Cursor cursor = database.rawQuery(sql, new String[] {});
		
		try{
			if(cursor.moveToFirst()){
				do{
					com = new Comment();
					com.setReciever(cursor.getString(cursor.getColumnIndex("reciever")));
					com.setUser(cursor.getString(cursor.getColumnIndex("sender")));
					com.setComment(cursor.getString(cursor.getColumnIndex("message")));
					
					list.add(com);
					cursor.moveToNext();
					
				}while(!cursor.isAfterLast());
			}
			
			/*else
			{
				com = new Comment();
			com.setComment("empty");
			com.setReciever("jill");
			com.setUser("Bill");
			
			list.add(com);
			}
			*/
			
			
		}catch (RuntimeException e)     
		{
			cursor.close();
			Log.e("PopulateMessage", e.toString(), e);
		}
		cursor.close();
		return list;
	}
	
	public void deleteConversation(String user, String buddy){
		 database.delete("Messages", "sender = '"+user+"' and reciever = '"+buddy+"'", null);
		 database.delete("Messages", "sender = '"+buddy+"' and reciever = '"+user+"'", null);
	}
	
}
